<template>
  <div class="jsbos-common-layout">
    <div class="jsbos-common-layout-center">
      <el-row class="jsbos-common-search-box" :gutter="16">
        <el-form @submit.native.prevent>
          <el-col :span="4">
            <el-form-item label="投诉类型">
              <el-select
                v-model="query.workOrderTypeId"
                placeholder="请选择"
                clearable
              >
                <el-option
                  v-for="(item, index) in workOrderTypeIdOptions"
                  :key="index"
                  :label="item.fullName"
                  :value="item.enCode"
                  :disabled="item.disabled"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="投诉人">
              <el-input
                v-model="query.contacts"
                placeholder="请输入"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="投诉科室">
              <dep-select
                v-model="query.location"
                placeholder="请选择
                                        "
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="search()"
                >查询</el-button
              >
              <el-button icon="el-icon-refresh-right" @click="reset()"
                >重置</el-button
              >
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="jsbos-common-layout-main jsbos-flex-main">
        <div class="jsbos-common-head">
          <div>
            <el-button
              type="primary"
              icon="el-icon-plus"
              @click="addOrUpdateHandle()"
              >新增
            </el-button>
          </div>
          <div class="jsbos-common-head-right">
            <el-tooltip effect="dark" content="刷新" placement="top">
              <el-link
                icon="icon-ym icon-ym-Refresh JSBOS-common-head-icon"
                :underline="false"
                @click="reset()"
              />
            </el-tooltip>
            <screen-full isContainer />
            <el-tooltip effect="dark" content="列设置" placement="top">
              <el-link
                icon="icon-ym icon-ym-options jsbos-common-head-icon"
                :underline="false"
                @click="() => this.$refs.jsbosTableRef.showDrawer()"
              />
            </el-tooltip>
          </div>
        </div>
        <el-tabs
          v-model="activeName"
          @tab-click="handleClick"
          style="margin-left: 15px"
        >
          <el-tab-pane
            v-for="item in databaseNameOptions"
            :label="item.fullName"
            :key="item.enCode"
            :name="item.enCode"
          >
            <template slot="label">
              {{ item.fullName }}
              <el-badge
                v-if="draftCount > 0 && item.enCode == 'draft'"
                :value="draftCount"
                size="mini"
              ></el-badge>
            </template>
          </el-tab-pane>
        </el-tabs>
        <jsbos-table
          v-loading="listLoading"
          :data="list"
          :border="true"
          ref="jsbosTableRef"
          @sort-change="sortChange"
        >
          <!-- <el-table-column prop="code" label="工单编号" width="0" align="left" /> -->
          <el-table-column
            prop="code"
            label="工单编号"
            width="150"
            align="left"
          >
            <template slot-scope="scope">
              <el-link
                type="primary"
                :underline="false"
                @click="goDetail(scope.row)"
              >
                {{ scope.row.code }}
              </el-link>
            </template>
          </el-table-column>
          <el-table-column
            prop="title"
            label="工单标题"
            width="300"
            align="left"
          />
          <el-table-column
            prop="workOrderTypeId"
            label="投诉类型"
            width="0"
            align="left"
          />
          <el-table-column
            prop="contacts"
            label="投诉人"
            width="0"
            align="left"
          />
          <el-table-column
            prop="contactsPhone"
            label="投诉电话"
            width="0"
            align="left"
          />
          <el-table-column
            prop="location"
            label="投诉科室"
            width="0"
            align="left"
          />
          <el-table-column
            label="投诉方式"
            width="0"
            prop="putway"
            algin="left"
          >
            <template slot-scope="scope">
              {{ scope.row.putway | dynamicText(putwayOptions) }}
            </template>
          </el-table-column>
          <el-table-column
            prop="priority"
            label="优先级"
            width="0"
            align="left"
          />
          <el-table-column
            prop="note"
            label="投诉内容描述"
            width="0"
            align="left"
          />
          <el-table-column label="操作" width="180" fixed="right">
            <template slot-scope="scope">
              <el-button
                type="text"
                @click="dataFormSubmit(scope.row.id)"
                v-if="scope.row.enabledMark == 'DRAFT'"
                >提交</el-button
              >
              <el-button
                type="text"
                @click="addOrUpdateHandle(scope.row.id)"
                v-if="scope.row.enabledMark == 'DRAFT'"
                >编辑</el-button
              >
              <el-button
                type="text"
                class="jsbos-table-delBtn"
                @click="handleDel(scope.row.id)"
                v-if="scope.row.enabledMark == 'DRAFT'"
                >删除</el-button
              >
              <el-button
                type="text"
                @click="assign(scope.row.id)"
                v-if="scope.row.enabledMark == 'UNASSIGNED'"
                >分派</el-button
              >
              <el-button
                type="text"
                @click="executeOrder(scope.row.id)"
                v-if="scope.row.enabledMark == 'EXECUTING'"
                >执行</el-button
              >
              <el-button
                type="text"
                @click="evaluate(scope.row.id, scope.row.enabledMark)"
                v-if="scope.row.enabledMark == 'EVALUATED'"
                >评价</el-button
              >
              <el-button type="text" @click="goDetail(scope.row)"
                >详情</el-button
              >
            </template>
          </el-table-column>
        </jsbos-table>
        <pagination
          :total="total"
          :page.sync="listQuery.currentPage"
          :limit.sync="listQuery.pageSize"
          @pagination="initData"
        />
      </div>
    </div>
    <JSBOS-Form v-if="formVisible" ref="JSBOSForm" @refresh="refresh" />
    <ExportBox v-if="exportBoxVisible" ref="ExportBox" @download="download" />
    <Detail
      v-if="detailVisible"
      ref="Detail"
      @refresh="detailVisible = false"
    />
    <Assign v-if="assignDialog" ref="assignRef" @close="refresh" />
    <execute v-if="executeDialog" ref="executeRef" @close="refresh" />
    <Evaluate v-if="evaluateDialog" ref="evaluateRef" @close="refresh" />
  </div>
</template>

<script>
import request from "@/utils/request";
import JSBOSForm from "./Form";
import ExportBox from "./ExportBox";
import Evaluate from "./evaluate.vue";
import Assign from "./assign.vue";
import execute from "./execute.vue";
import { getDictionaryDataSelector } from "@/api/systemData/dictionary";
import { getDataInterfaceRes } from "@/api/systemData/dataInterface";
import {
  getEnableMarkCount,
  updateEnabledMark,
  executeOrder,
} from "@/views/questionnaire/api/complaint";
import Detail from "./Detail";

export default {
  name: "OqComplaintWorkOrder",
  components: { JSBOSForm, ExportBox, Detail, Assign, execute, Evaluate },
  data() {
    return {
      activeName: "",
      draftCount: 0,
      databaseNameOptions: [],
      detailVisible: false,
      assignDialog: false,
      executeDialog: false,
      evaluateDialog: false,
      query: {
        workOrderTypeId: undefined,
        contacts: undefined,
        location: undefined,
      },
      treeProps: {
        children: "children",
        label: "fullName",
        value: "id",
      },
      list: [],
      listLoading: true,
      total: 0,
      listQuery: {
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: "",
      },
      formVisible: false,
      exportBoxVisible: false,
      columnList: [
        { prop: "workOrderTypeId", label: "投诉类型" },
        { prop: "contacts", label: "投诉人" },
        { prop: "contactsPhone", label: "投诉电话" },
        { prop: "location", label: "投诉科室" },
        { prop: "putway", label: "投诉方式" },
        { prop: "priority", label: "优先级" },
        { prop: "note", label: "投诉内容描述" },
      ],
      workOrderTypeIdOptions: [],
      workOrderTypeIdProps: {
        label: "fullName",
        value: "enCode",
      },
      putwayOptions: [],
      putwayProps: {
        label: "fullName",
        value: "enCode",
      },
      priorityOptions: [],
      priorityProps: {
        label: "fullName",
        value: "enCode",
      },
    };
  },
  computed: {
    menuId() {
      return this.$route.meta.modelId || "";
    },
  },
  created() {
    this.initData();
    this.getWorkOrderTypeIdOptions();
    this.getDictionary();
  },
  methods: {
    handleClick(tab, event) {
      this.listQuery.enabledMark = tab.name;
      console.log(tab.name, event);
      this.initData();
    },
    getEnableMarkCount() {
      var data = {
        enabledMark: "DRAFT",
      };
      getEnableMarkCount(data).then((res) => {
        this.draftCount = res.data;
      });
    },
    getWorkOrderTypeIdOptions() {
      this.$store
        .dispatch("base/getDictionaryData", {
          encode: "complaint",
        })
        .then((res) => {
          this.workOrderTypeIdOptions = res;
        });
    },
    goDetail(row) {
      this.detailVisible = true;
      this.$nextTick(() => {
        this.$refs.Detail.init(row);
      });
    },
    getDictionary() {
      this.$store
        .dispatch("base/getDictionaryData", {
          encode: "ComplaintStatus",
        })
        .then((res) => {
          this.databaseNameOptions = [{ fullName: "全部", enCode: "" }];
          this.databaseNameOptions.push(...res);
        });
    },
    executeOrder(id) {
      this.executeDialog = true;
      this.$nextTick(() => {
        this.$refs.executeRef.init(id);
      });
      // this.$confirm("确定执行?", "提示", {
      //     confirmButtonText: "确定",
      //     cancelButtonText: "取消",
      //     type: "warning",
      // })
      //     .then(() => {
      //         var data = {
      //             id: id,
      //         };
      //         executeOrder(data).then((res) => {
      //             this.$message.success(res.msg);
      //             this.initData();
      //         });
      //     })
      //     .catch(() => { });
    },
    dataFormSubmit(id) {
      this.$confirm("确定提交?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          var data = {
            id: id,
            enabledMark: "UNASSIGNED",
          };
          updateEnabledMark(data).then((res) => {
            if (res.code == 400) {
              this.$message.success(res.msg);
            } else {
              this.$message.success("提交成功！");
            }
            this.initData();
          });
        })
        .catch(() => {});
    },
    sortChange({ column, prop, order }) {
      this.listQuery.sort = order == "ascending" ? "asc" : "desc";
      this.listQuery.sidx = !order ? "" : prop;
      this.initData();
    },
    initData() {
      this.listLoading = true;
      let _query = {
        ...this.listQuery,
        ...this.query,
        menuId: this.menuId,
      };
      request({
        url: `/api/questionnaire/OqComplaintWorkOrder/getList`,
        method: "post",
        data: _query,
      }).then((res) => {
        var _list = [];
        for (let i = 0; i < res.data.list.length; i++) {
          let _data = res.data.list[i];
          _list.push(_data);
        }
        this.list = _list;
        this.total = res.data.pagination.total;

        this.listLoading = false;
        this.getEnableMarkCount();
      });
    },
    handleDel(id) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        type: "warning",
      })
        .then(() => {
          request({
            url: `/api/questionnaire/OqComplaintWorkOrder/${id}`,
            method: "DELETE",
          }).then((res) => {
            this.$message({
              type: "success",
              message: res.msg,
              onClose: () => {
                this.initData();
              },
            });
          });
        })
        .catch(() => {});
    },
    addOrUpdateHandle(id, isDetail) {
      this.formVisible = true;
      this.$nextTick(() => {
        this.$refs.JSBOSForm.init(id, isDetail);
      });
    },
    exportData() {
      this.exportBoxVisible = true;
      this.$nextTick(() => {
        this.$refs.ExportBox.init(this.columnList);
      });
    },
    // 评价
    evaluate(id, enabledMark) {
      this.evaluateDialog = true;
      this.$nextTick(() => {
        this.$refs.evaluateRef.init(id, enabledMark);
      });
    },
    //分派
    assign(id, enabledMark, type) {
      this.assignDialog = true;
      this.$nextTick(() => {
        this.$refs.assignRef.init(id, enabledMark, type);
      });
    },
    download(data) {
      let query = {
        ...data,
        ...this.listQuery,
        ...this.query,
        menuId: this.menuId,
      };
      request({
        url: `/api/questionnaire/OqComplaintWorkOrder/Actions/Export`,
        method: "GET",
        data: query,
      }).then((res) => {
        if (!res.data.url) return;
        this.jsbos.downloadFile(res.data.url);
        this.$refs.ExportBox.visible = false;
        this.exportBoxVisible = false;
      });
    },
    search() {
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: "",
      };
      this.initData();
    },
    refresh(isrRefresh) {
      this.formVisible = false;
      if (isrRefresh) this.reset();
    },
    reset() {
      for (let key in this.query) {
        this.query[key] = undefined;
      }
      this.search();
    },
  },
};
</script>
